<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<div id="rightinfo" class="rightinfo"></div>

<!-- 模板 -->
<script id="list" type="text/html">
    <ul class="seachform">
    <li><input id="name" type="text" class="scinput outinput" value="备份名称" /></li>
    <li><input type="button" class="scbtn create" value="创建备份"/></li>
    </ul>
    <table class="tablelist">
        <thead>
        <tr>
        <th>备份名称</th>
        <th style="width:12%">大小</th>
        <th style="width:18%">备份日期</th>
        <th style="width:15%">操作</th>
        </tr>
        </thead>
        <tbody>
        <%for(i = 0; i < list.length; i ++) {%>
            <tr <%=#(i%2 ==0) ?'':'class="odd"'%> data-id="<%=list[i].name%>">
            <td><%=list[i].name%></td>
            <td><%=list[i].size%></td>
            <td><%=list[i].date%></td>
            <td>
                <a class="tablelink act_restore">恢复</a>
                <a class="tablelink" href="<?php echo site_url('ajax/backup_handle/download');?>?name=<%=list[i].name%>" target="_blank">下载</a>
                <a class="tablelink act_delete">删除</a>
            </td>
            </tr>
        <%}%>        
        </tbody>
    </table>  
    <div class="clearfloat"></div>
</script>

<!-- 脚本 -->
<script src="js/template-helper.js" type="text/javascript"></script>
<script type="text/javascript">
function get_page(){
    $.getJSON(
        '<?php echo site_url('ajax/backup_handle/get_list');?>',
        {},
        function(rs){
            if(rs.err===0){
                //呈现内容
                $('#rightinfo').html(template('list', rs.data));
                
            }else{
                  layer.msg(rs.msg, {icon:2,time:2000});
            }            
        }
    );
};
//初始化
get_page();

$('#rightinfo').on('focus','#name',function(){
	$(this).removeClass('outinput');
	if($(this).val()=='备份名称'){
		$(this).val('');
	}
}).on('blur','#name',function(){
	if($(this).val()==''){
		$(this).addClass('outinput');		
		$(this).val('备份名称');
	}
});

//备份
$('#rightinfo').on('click', '.create', function(){
	var index = layer.load();
    $.post(
            '<?php echo site_url('ajax/backup_handle/create');?>',
            {
                  name:$('#name').val()
            },
            function(rs){
            	layer.close(index);
                if(rs.err===0){
                    layer.msg('备份成功！', {icon:1,time:2000});    
                    get_page();
                }else{
                    layer.msg(rs.msg, {icon:2,time:2000});
                }
            },
            'json'
    );
});
//恢复
$('#rightinfo').on('click', '.act_restore', function(){
    var name = $(this).parent().parent().attr('data-id');
    var confirm = layer.confirm('此操作将重建数据库，请确认已经备份当前数据，是否继续？', {icon: 0, title:'提示', btn: ['是','否']}, function(){
        layer.close(confirm);
        var load = layer.load();
        $.post(
                '<?php echo site_url('ajax/backup_handle/restore');?>',
                {
                      name:name,
                      token:'<?php echo isset($user['CSRF'])?$user['CSRF']:'';?>'
                },
                function(rs){
                	layer.close(load);
                    if(rs.err===0){
                        layer.msg('恢复成功！', {icon:1,time:2000}, function(){
                            location.href = location.href;
                        });
                    }else{
                        layer.msg(rs.msg, {icon:2,time:2000});
                    }
                },
                'json'
        );
    });
});
//删除
$('#rightinfo').on('click', '.act_delete',function(){
	var name = $(this).parent().parent().attr('data-id');    
	layer.confirm('确定删除？', {icon: 3, title:'提示', btn: ['是','否']}, function(){
	    $.post(
	            '<?php echo site_url('ajax/backup_handle/delete');?>',
	            {
	                  name:name,
                      token:'<?php echo isset($user['CSRF'])?$user['CSRF']:'';?>'
	            },
	            function(rs){
	                if(rs.err===0){
	                    layer.msg('删除成功！', {icon:1,time:2000});    
	                    get_page();
	                }else{
	                    layer.msg(rs.msg, {icon:2,time:2000});
	                }
	            },
	            'json'
	    );
    });
});
</script>